package com.car.service;

import com.car.dao.CarDao;
import com.car.dao.PurchaseRecordDao;
import com.car.dao.UserCarDao;
import com.car.dao.UserDao;
import com.car.dao.impl.CarDaoImpl;
import com.car.dao.impl.PurchaseRecordDaoImpl;
import com.car.dao.impl.UserCarDaoImpl;
import com.car.domain.Car;
import com.car.domain.PurchaseRecord;
import com.car.domain.User;
import com.car.domain.UserCar;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

/**
 * 用户业务服务类
 */
public class UserService {
    private final UserDao userDao;
    private final static UserCarDao userCarDao = new UserCarDaoImpl();
    private final static CarDao carDao = new CarDaoImpl();
    private final static PurchaseRecordDao purchaseRecordDao = new PurchaseRecordDaoImpl();

    public UserService(UserDao userDao) {
        this.userDao = userDao;
    }

    /**
     * 用户 注册
     */
    public boolean register() {
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String userName = scanner.next();
        user.setName(userName);
        System.out.println("请输入电话号码：");
        String phone = scanner.next();
        user.setPhone(phone);
        System.out.println("请输入密码：");
        String password = scanner.next();
        user.setPassword(password);
        System.out.println("请输入邮箱：");
        String email = scanner.next();
        user.setEmail(email);
        return userDao.register(user);
    }

    /**
     * 用户 登录
     * 输入 手机号  和 密码
     *
     * @return 登录的用户
     */
    public User login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入电话号码：");
        String phone = scanner.next();
        System.out.println("请输入密码：");
        String password = scanner.next();
        return userDao.login(phone, password);
    }

    /**
     * 自己的汽车
     *
     * @param user 用户
     * @return 汽车
     */
    public List<Car> ownCars(User user) {
        // select tc.* from t_user_car tuc left join t_car tc on tuc.car_id = tc.id where tuc.user_id = ?
        return userDao.ownCars(user.getId());
    }

    /**
     * 购买汽车
     *
     * @param user    用户
     * @param scanner scanner
     */
    public void buyCar(User user, Scanner scanner) throws Exception {
        System.out.println("购买汽车");
        // 让用户输入 购买车的id
        System.out.println("请输入购买汽车的id：");
        int carId = scanner.nextInt();
        // 在数据库查询 汽车是否存在 并且 状态未销售 status 0未销售 1已经销售
        Car car = carDao.findByIdAndStatusEq0(carId);
        // 如果在数据库中没有查询到 购买失败 退出
        if (car == null) {
            System.out.println("购买失败");
            return;
        }
        // 汽车状态更新 status 0 -> 1
        car.setStatus(1);
        carDao.update(car);
        // 汽车和用户的中间表 新增一条记录
        UserCar userCar = new UserCar();
        userCar.setCarId(carId);
        userCar.setUserId(user.getId());
        userCarDao.add(userCar);
        // 销售记录表 新增一条记录
        PurchaseRecord pr = new PurchaseRecord();
        pr.setCarId(carId);
        pr.setUserId(user.getId());
        pr.setEmployeeId(car.getEmployeeId());
        pr.setPurchaseDatetime(LocalDateTime.now());
        purchaseRecordDao.add(pr);
    }

    public void updatePassword(User user, Scanner scanner) throws Exception {
        System.out.println("修改密码");
        System.out.println("请输入新密码：");
        String password = scanner.next();
        user.setPassword(password);
        int i = userDao.update(user);
        if (i > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("操作失败");
        }
    }

    public void updateUsername(User user, Scanner scanner) throws Exception {
        System.out.println("修改用户名");
        System.out.println("请输入新的用户名：");
        String username = scanner.next();
        user.setName(username);
        int i = userDao.update(user);
        if (i > 0) {
            System.out.println("更新成功");
        } else {
            System.out.println("操作失败");
        }
    }
}
